package leo.game.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import leo.data.GameConfig;
	import leo.data.GameConst;
	import leo.data.GameModel;
	import leo.data.MonsterVO;
	import leo.game.stage.Game;
	import leo.game.ui.UILife;
	import leo.game.ui.UILose;
	import leo.game.ui.UIMap;
	import leo.manager.SoundManager;
	import leo.manager.WinOrLoseManager;
	import leo.util.Animation;
	import leo.util.GameUtil;
	
	/**
	 * 怪物 
	 * @author leo
	 * 
	 */	
	public class BaseMonster extends MapObject
	{
		
		/**
		 * uid值 
		 */		
		public var uid :String;
		/**
		 * 是否存活 
		 */		
		public var isAlive :Boolean;
		/**
		 * 鼠标On 
		 */		
		public var mouseOn :Boolean;
		/**
		 * 使用的位图 
		 */		
		public var bitmap :Bitmap;
		/**
		 * 怪物VO 
		 */		
		public var monsterVO :MonsterVO;
		/**
		 * 子弹结束点 
		 */		
		public var bulletEndPoint :Point;
		/**
		 * 生命条 
		 */		
		public var life :UILife;
		/**
		 * 怪物当前血量 
		 */		
		public var curHP :int;
		/**
		 * 是否已经减速 
		 */		
		public var isDropSpeed :Boolean = false;
		/**
		 * 减速时间 
		 */		
		public var dropSpeedTime :int;
		/**
		 * 是否已经加入到地图 
		 */		
		public var isAdd2Map :Boolean = false;
		/**
		 * 当前行走方向 
		 */		
		public var curWalkDir :int;
		/**
		 * 当前播放到那一帧 
		 */		
		private var curFrame :int = 0;
		/**
		 * 当前动作的位图列表 
		 */		
		private var _curActionBMDAry :Vector.<BitmapData>;
		/**
		 * 怪物的行走路径 
		 */		
		private var path :Array;
		/**
		 *  怪物行走路径数组的索引
		 */		
		private var index :int = 0;
		/**
		 * 怪物动画播放换图和行走时间间隔90毫秒 
		 */		
		private const WALKTIME :int = 90;
		/**
		 * 播放动画刷新时间 
		 */		
		private var refreshTime :int = 0;
		/**
		 * 行走刷新时间 
		 */		
		private var walkRefreshTime :int = 0;
		/**
		 * 转向旧值，用于调整血条显示位置 
		 */		
		private var oldRotationValue :int;
		/**
		 * 被减速前的行走值 
		 */		
		private var walkSpeed :int;
		/**
		 * 是否已经走到终点 
		 */		
		private var isWalk2End :Boolean;


		public function BaseMonster(vo :MonsterVO,delay :int = 500)
		{
			super();
			
			this.monsterVO = vo;
			this.isAlive = true;
			this.uid = "m" + int(1000 * Math.random());
			this.bulletEndPoint = new Point();
			this.life = new UILife();
			this.life.x = 16;
			this.life.y = 2;
			this.walkSpeed = this.monsterVO.walkSpeed;
			this.curHP = this.monsterVO.hp;
			this.addChild(this.life);
			this.bitmap = new Bitmap();
			this.addChild(bitmap);

			//寻路
			var result :Array = UIMap.instance.path;
			this.path = GameUtil.instance.copyAry(result);
			this.curActionBMDAry = vo.bmdDict[this.getDir()/*monsterVO.dir_right*/ * monsterVO.ratio]; 
			
		}
		
		public override function update():void
		{
			if(!this.isAlive) return;
			super.update();
			walk();
			animater();
			
			if(this.isDropSpeed)
			{
				this.dropSpeedTime += Game.instance.refreshInterval;
				if(this.dropSpeedTime >= GameConfig.drop_speed_time)
				{
					this.isDropSpeed = false;
					this.dropSpeedTime = 0;
					this.monsterVO.walkSpeed = this.walkSpeed;
					this.filters = [];
				}
			}
		}
		/**
		 * 扣减血条 
		 * @param value
		 * 
		 */		
		public function dropHP(att :int):void
		{
			var value :Number = ((GameConst.TILE_WIDTH/2)* att)/this.monsterVO.hp; 
			if(this.curHP <= 0)
			{
				this.life.destroy();
				this.removeChild(this.life);
				this.life = null;
				return;
			}
			this.life.updateLife(value);
		}
		/**
		 * 改变怪物速度 
		 * 
		 */		
		public function changeSpeed():void
		{
			if(this.isDropSpeed) return;
			
			this.isDropSpeed = true;
//			this.monsterVO.walkSpeed *= GameConfig.drop_speed;
			this.monsterVO.walkSpeed = 0;
			this.filters = [GameConst.MONSTER_IS_DROP_SPEED];
		}
		/**
		 * 当前动作的位图列表 
		 * @return 
		 * 
		 */		
		public function get curActionBMDAry():Vector.<BitmapData>
		{
			return _curActionBMDAry;
		}
		/**
		 * 当前动作的位图列表 
		 * @param value
		 * 
		 */		
		public function set curActionBMDAry(value:Vector.<BitmapData>):void
		{
			_curActionBMDAry = value;
		}
		/**
		 * 播放动画 
		 * @param isPlayNow  是否立刻播放
		 */		
		private function animater(isPlayNow :Boolean = false):void
		{
			if(!this.isAlive) return;
			this.refreshTime += Game.instance.refreshInterval;
			if(this.refreshTime >= this.WALKTIME)
			{
				if(this.curActionBMDAry.length > 0)
				{
					this.curFrame++;
					if(curFrame >= this.curActionBMDAry.length)
					{
						curFrame = 0;
						this.bitmap.bitmapData = this.curActionBMDAry[curFrame];
						return;
					}
					this.bitmap.bitmapData = this.curActionBMDAry[curFrame] as BitmapData;
				}
				this.refreshTime = 0;
			}
			
		}
		/**
		 * 加入到地图 
		 * @return 
		 * 
		 */		
		private function add():Boolean
		{
			var isOver :Boolean = GameModel.instance.isOver;
			if(isOver)
			{
				return false;
			}
			UIMap.instance.addItem(this);
			return true;
		}
		/**
		 * 怪物行走 
		 * 
		 */		
		private function walk():void
		{
			if(this.isWalk2End) return;
			if(!this.isAlive) return;
			if(this.path && this.path.length > 0)
			{
				this.walkRefreshTime += Game.instance.refreshInterval;
				if(this.walkRefreshTime >= this.WALKTIME)
				{
					if(this.index > this.path.length - 1)
					{
						//游戏结束条件
						trace("到达终点");
						this.isWalk2End = true;
						this.isAlive = false;
						GameModel.instance.curMapHp -= 1;
						if(GameModel.instance.curMapHp <= 0)
						{
							WinOrLoseManager.instance.lose();
							SoundManager.instance.play("FailMusic");
						}
						return;
					}
					
					var p :* = this.path[this.index];
					p = GameUtil.instance.indexToPixel(p);
					
					var dx :int = p.x - this.x;
					var dy :int = p.y - this.y;
					
					if((Math.abs(dx) + Math.abs(dy)) <= 1)
					{
						this.index++;
						return;
					}
					var angle :Number = Math.atan2(dy,dx);
					var dirX :Number = (Math.cos(angle));
					var dirY :Number = (Math.sin(angle));
					this.x = (this.x + this.monsterVO.walkSpeed * dirX);
					this.y = (this.y + this.monsterVO.walkSpeed * dirY);
					
					this.bulletEndPoint.x = this.x + 32;
					this.bulletEndPoint.y = this.y + 32;


					var monsterRotation :int = angle/Math.PI*180-90;
					if(monsterRotation == -180)  //上
					{
						this.life.x = 16;
						this._curActionBMDAry = this.monsterVO.bmdDict[this.monsterVO.dir_up * this.monsterVO.ratio];
						this.curWalkDir = this.monsterVO.dir_up;
					}else if(monsterRotation == 0) //下
					{
						this.life.x = 16;
						this._curActionBMDAry = this.monsterVO.bmdDict[this.monsterVO.dir_down * this.monsterVO.ratio];
						this.curWalkDir = this.monsterVO.dir_down;
					}else if(monsterRotation == 90)  //左
					{
						this.life.x = 20;
						this._curActionBMDAry = this.monsterVO.bmdDict[this.monsterVO.dir_left * this.monsterVO.ratio];
						this.curWalkDir = this.monsterVO.dir_left;
					}else if(monsterRotation == -90)  //右
					{
						this.life.x = 20;
						this._curActionBMDAry = this.monsterVO.bmdDict[this.monsterVO.dir_right * this.monsterVO.ratio];
						this.curWalkDir = this.monsterVO.dir_right;
					}
					
					this.walkRefreshTime = 0;

				}
			}
		}
		/**
		 * 获取初始方向 
		 * @return 
		 * 
		 */		
		private function getDir():int
		{
			var value :int = this.monsterVO.dir_right;
			if(this.path && this.path.length > 0)
			{
				var p :* = this.path[0];
				p = GameUtil.instance.indexToPixel(p);
				
				var dx :int = p.x - this.x;
				var dy :int = p.y - this.y;
				
				var angle :Number = Math.atan2(dy,dx);
				var monsterRotation :int = angle/Math.PI*180-90;
				
				if(monsterRotation == -180)  //左？
				{
					value = this.monsterVO.dir_left;
				}else if(monsterRotation == 0) //右？
				{
					value = this.monsterVO.dir_right;
				}else if(monsterRotation == 90)  //上？
				{
					value = this.monsterVO.dir_up;
				}else if(monsterRotation == -90)  //下？
				{
					value = this.monsterVO.dir_down;
				}
			}
			return value;
		}
		
		/**
		 * 硬转换怪物转向角度 
		 * @param r
		 * @return 
		 * 
		 */		
		private function changeMonsterRotion(r :int):int
		{
			var value :int;
			if(r == 0)
			{
				value = 0;
			}else if(r > 0 && r <= 90)
			{
				value = 90;
			}else if(r < 0 && r >= -90)
			{
				value = -90;
			}else if(r < 0 && r < -90)
			{
				value = -180;
			}
			
			return value;
		}

	}
}